package com.kylin.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kylin.JwtUtils;
import com.kylin.MD5;
import com.kylin.educenter.entity.UcenterMember;
import com.kylin.educenter.entity.vo.RegisterVo;
import com.kylin.educenter.mapper.UcenterMemberMapper;
import com.kylin.educenter.service.UcenterMemberService;
import com.kylin.handler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author kylin
 * @since 2020-07-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {


    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(UcenterMember member) {

        //获取手机号和密码
        String phone = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败,账号或密码错误");
        }

        //查询数据库
        //根据手机号查询对象  判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", phone);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (StringUtils.isEmpty(mobileMember)) {
            throw new GuliException(20001, "登陆失败，手机号不存在！");
        }

        //判断密码
        //存储在数据库中的密码是加密的，把输入的密码进行加密,再和数据库密码比较
        //使用MD5加密
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001, "登录失败,账号或密码错误");
        }
        //判断是否禁用
        if (mobileMember.getIsDisabled()) {
            throw new GuliException(20001, "登录失败!");
        }

        //登录成功
        //生成token字符串
        String token = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return token;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {

        //获取手机注册信息
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//用户名
        String password = registerVo.getPassword();

        if (StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            throw new GuliException(2001,"注册失败");
        }

        //判断手机验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);//获取发送后存放在redis中的验证码
        if (!code.equals(redisCode)){
            throw new GuliException(2001,"注册失败");
        }

        //判断手机号是否重复,表里面是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);

        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            //表示表里存在该手机记录
            throw new GuliException(2001,"注册失败");
        }

        //数据添加到数据库中
        UcenterMember ucenterMember = new UcenterMember();
        //密码加密
        String mdPassword = MD5.encrypt(password);
        registerVo.setPassword(mdPassword);
        BeanUtils.copyProperties(registerVo,ucenterMember);
        ucenterMember.setAvatar("https://edu-kylincw.oss-cn-beijing.aliyuncs.com/2020/07/14/16d82a98dae049c688995274062a6cb0file.png");//默认头像
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {


        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);

        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);


        return ucenterMember;
    }

    //查询某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {


        return  baseMapper.countRegisterDay(day);
    }
}
